package com.jdbc.dao;

import com.jdbc.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class AccountDaoImpl implements AccountDao {
    //定义JdbcTemplate对象
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //按id查询数据表account
    @Override
    public Account findAccountById(int id) {
        String sql = "select * from spring.account where id=?";
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class);
        Account account = jdbcTemplate.queryForObject(sql, rowMapper, id);
        return account;
    }

    //查询数据表account所有记录
    @Override
    public List<Account> findAllAccount() {
        String sql = "select * from spring.account";
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<>(Account.class);
        List<Account> accounts = jdbcTemplate.query(sql, rowMapper);
        return accounts;
    }
}
